﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace FasterWPF
{
    public class StyleMultiConverter: IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            return values[0];
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public static class StyleDataBinding
    {
        #region DataBinding to Value Via a Style

        /// <summary>
        /// DataBind One Child's Property (Child K) to Property on Framework Element Parameter via Style
        /// </summary>
        /// <typeparam name="K"></typeparam>
        /// <typeparam name="FE"></typeparam>
        /// <param name="child"></param>
        /// <param name="fe"></param>
        /// <param name="depPropertyOnK"></param>
        /// <param name="depPropertyOnFEThatKBindsTo"></param>
        /// <param name="uSourceTrigger"></param>
        /// <param name="bMode"></param>
        public static void DataBindOneChildsPropertyToFEViaStyle<K, FE>(this K child, FrameworkElement fe, DependencyProperty depPropertyOnK, DependencyProperty depPropertyOnFEThatKBindsTo, UpdateSourceTrigger uSourceTrigger, BindingMode bMode)
            where K : FrameworkElement
            where FE : FrameworkElement
        {
            MultiBinding mbi = new MultiBinding();
            mbi.Converter = new StyleMultiConverter();
            mbi.Bindings.Add(
            new Binding
            {
                Source = fe as FE,
                Path = new PropertyPath(depPropertyOnFEThatKBindsTo),
                Mode = bMode,
                UpdateSourceTrigger = uSourceTrigger
            }
            );

            Style style = new Style(typeof(K), child.Style);

            Setter setter = new Setter();
            setter.Property = depPropertyOnK;
            setter.Value = mbi;
            style.Setters.Add(setter);
            child.Style = style;
        }
        #endregion
    }
}
